package com.moonshineproject.plugin.actionscript.as3parser.swcparser
{
	import com.moonshineproject.plugin.actionscript.as3parser.vo.ClassVO;
	import com.moonshineproject.plugin.actionscript.as3parser.vo.FunctionVO;
	import com.moonshineproject.plugin.actionscript.as3parser.vo.PackageVO;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	public class SWCDataSerializer
	{
		
		public static function getCacheDir():File
		{   
			var f:File = File.applicationStorageDirectory.resolvePath("swc_cache");
			if (!f.exists) f.createDirectory();
			
			return f;
		}
		
		public function SWCDataSerializer()
		{
			
		}
		
		public function deserialize(cacheFile:File):PackageVO
		{			
			var now:int = flash.utils.getTimer();
			
			var fr:FileStream = new FileStream();
			fr.open(cacheFile, FileMode.READ);
			
			var ba:ByteArray = new ByteArray();
			
			fr.readBytes(ba);
			fr.close();
			
			ba.position = 0;
			var pkg:PackageVO = ba.readObject() as PackageVO;
			
			trace("SWF cache load took: " + (flash.utils.getTimer() - now) + " ms");
			
			return pkg;
		}
		
		public function serialize(filename:String, data:PackageVO):void
		{
			var dir:File = getCacheDir();
			var cacheFile:File = dir.resolvePath(filename);
			
			var ba:ByteArray = new ByteArray();
			ba.writeObject(data);
			
			var fw:FileStream = new FileStream();
			fw.open(cacheFile, FileMode.WRITE);
			fw.writeBytes(ba);
			fw.close();
		}
		
	}
}